package com.clkj.zhonghui.controller.pay;

import com.clkj.zhonghui.pojo.common.Result;
import com.clkj.zhonghui.pojo.common.StatusCode;
import com.clkj.zhonghui.service.order.OrderService;
import com.clkj.zhonghui.service.pay.PayService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * @author yawang
 * @create 2024/07/11 16:07:46
 **/
@RestController
@RequestMapping(value = "/pay")
@Api(value = "/pay", tags = {"支付管理"})
@CrossOrigin
public class PayController {
    @Autowired
    private PayService payService;
    @PostMapping("/pay")
    @ApiOperation(value = "app--订单支付")
    public Result pay(@ApiParam(name="userId",value = "用户ID")@RequestParam Integer userId,
                      @ApiParam(name="shopId",value = "门店ID")@RequestParam Integer shopId,
                      @ApiParam(name="orderNumber",value = "订单号")@RequestParam String orderNumber,
                      @ApiParam(name = "parentId", value = "父级ID,没有传0，如果是3650活动的首次购买鸡胚胎，传-1，用券购买传-2") @RequestParam("parentId") Integer parentId,
                      @ApiParam(name = "realMoney", value = "支付金额") @RequestParam("realMoney") String realMoney,
                      @ApiParam(name = "rebate", value = "给上级的返利") @RequestParam("rebate") Double rebate,
                      @ApiParam(name = "flag", value = "0为app支付 1为微信支付") @RequestParam("flag") Integer flag,
                      @ApiParam(name = "openId", value = "app传0") @RequestParam("openId") String  openId,
                      @ApiParam(name = "ifDown", value = "是否下载app") @RequestParam("ifDown") Integer  ifDown,

                      HttpServletRequest request){
        try {
            Map map  =payService.pay(userId,shopId,orderNumber,parentId,realMoney,rebate,flag,openId,ifDown,request);
            return new Result(true, StatusCode.OK, "更新成功",map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "更新失败");
        }
    }


    @PostMapping("/payOrderSuccess")
    @ApiOperation(value = "app--订单支付回调")
    public Result payOrderSuccess(HttpServletRequest request, HttpServletResponse response){
        payService.payOrderSuccess(request,response);
        return new Result(true, StatusCode.OK, "更新成功");
    }

    @PostMapping("/payMiniOrder")
    @ApiOperation(value = "小程序--购买套餐订单支付")
    public Result payMiniOrder(@ApiParam(name = "userId", value = "用户ID") @RequestParam Integer userId,
                               @ApiParam(name = "shopId", value = "门店ID") @RequestParam Integer shopId,
                               @ApiParam(name = "orderNumber", value = "订单号") @RequestParam String orderNumber,
                               @ApiParam(name = "parentId", value = "父级ID,没有传0") @RequestParam("parentId") Integer parentId,
                               @ApiParam(name = "highestId", value = "上上级ID,没有传0") @RequestParam("highestId") Integer highestId,
                               @ApiParam(name = "note", value = "自己是否是会员（会员为1，非会员为0.会员返利）") @RequestParam("note") Integer note,
                               @ApiParam(name = "realMoney", value = "支付金额") @RequestParam("realMoney") String realMoney,
                               @ApiParam(name = "rebate", value = "给上级的返利") @RequestParam("rebate") String rebate,
                               @ApiParam(name = "rebates", value = "给上上级的返利") @RequestParam("rebates") String rebates,
                               @ApiParam(name = "openId", value = "openId") @RequestParam("openId") String openId,
                               HttpServletRequest request) {
        try {
            Map map = payService.payMiniOrder(userId, shopId, orderNumber, parentId, highestId, note ,realMoney, rebate, rebates, openId,request);
            return new Result(true, StatusCode.OK, "更新成功", map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "更新失败");
        }
    }


    @PostMapping("/payMiniOrderSuccess")
    @ApiOperation(value = "小程序--购买套餐订单支付回调")
    public Result payMiniOrderSuccess(HttpServletRequest request, HttpServletResponse response){
        payService.payMiniOrderSuccess(request,response);
        return new Result(true, StatusCode.OK, "更新成功");
    }

    @PostMapping("/payMiniProductOrder")
    @ApiOperation(value = "小程序--中汇钛美购买普通商品订单支付")
    public Result payMiniProductOrder(@ApiParam(name = "userId", value = "用户ID") @RequestParam Integer userId,
                                      @ApiParam(name = "shopId", value = "门店ID") @RequestParam Integer shopId,
                                      @ApiParam(name = "orderNumber", value = "订单号") @RequestParam String orderNumber,
                                      @ApiParam(name = "realMoney", value = "支付金额") @RequestParam("realMoney") String realMoney,
                                      @ApiParam(name = "usedYh", value = "所用积分数") @RequestParam("usedYh") Double usedYh,
                                      @ApiParam(name = "openId", value = "openId") @RequestParam("openId") String openId,
                                      HttpServletRequest request) {
        try {
            Map map = payService.payMiniProductOrder(userId, shopId, orderNumber,realMoney, usedYh, openId,request);
            return new Result(true, StatusCode.OK, "更新成功", map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "更新失败");
        }
    }


    @PostMapping("/payMiniProductOrderSuccess")
    @ApiOperation(value = "小程序--中汇钛美购买普通商品订单支付回调")
    public Result payMiniProductOrderSuccess(HttpServletRequest request, HttpServletResponse response){
        payService.payMiniProductOrderSuccess(request,response);
        return new Result(true, StatusCode.OK, "更新成功");
    }

}
